package com.hyyt.shopjsp.order.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.export.ExportExcel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.order.pojo.ShopOrders;
import com.hyyt.shopjsp.order.pojo.ShopOrdersOplog;
import com.hyyt.shopjsp.order.service.IShopOrdersOplogService;
import com.hyyt.shopjsp.order.service.IShopOrdersService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/06/15 0024下午 3:56.
 */

@Controller
@RequestMapping("/back/ordersOPLog")
public class ShopOrdersOpLogController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopOrdersOpLogController.class);
	private String basePath = "/back/shop/order/";
	@Resource
	private IShopOrdersService shopOrdersService;//订单service接口
	@Resource
	private IShopOrdersOplogService shopOrdersOPLogService;//订单日志service接口

	/**
	 * 跳转订单日志列表页面
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoOrdersOPLogPage")
	public String gotoOrdersOPLogPage(){
		return basePath+"listOrdersOPLog";
	}

	/**
	 * 订单日志列表
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="ordersOPLogList",method=RequestMethod.POST)
	@ResponseBody
	public void ordersOPLogList(String ordersNo,String shopInfoType,Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(ordersNo)){
			params.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(shopInfoType)){
			params.put("shopInfoType", shopInfoType);
		}
		params.put("sortColumns", "createTime desc");
		int totalRecordCount =shopOrdersOPLogService.countShopOrdersOPLog(params);
		 page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		 List<ShopOrdersOplog> ordersOPLogList  =shopOrdersOPLogService.queryOrdersOPLog(params, page.getPageRecordBeginIndex(), page.getPageSize());
		 params.clear();
		 params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		 params.put("rows", ordersOPLogList);// rows键 存放每页记录 list
		 ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
     * 根据id获得对象
     *
     * @param ordersNo
     */
    @RequestMapping(value = "getOrdersOPLogObject", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg getOrdersOPLogObject(Integer ordersNo) {
		if (Utils.objectIsNotEmpty(ordersNo)) {
			ShopOrders shopOrders = shopOrdersService.loadShopOrders(ordersNo);//订单信息
			Map<String, Object> params=new HashMap<>();
			params.put("shopOrders", shopOrders);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(params));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 导出订单日志excel
	 * @param ordersNo
	 * @param shopInfoType
	 * @param response
	 * @param request
	 */
	@RequestMapping("exportOrdersOPLogExcel")
	public void exportOrdersOPLogExcel(String ordersNo,String shopInfoType, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(ordersNo)){
			params.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(shopInfoType)){
			params.put("shopInfoType", shopInfoType);
		}
		params.put("sortColumns", "createTime desc");
		List<ShopOrdersOplog> ordersOPLogList  =shopOrdersOPLogService.queryOrdersOPLog(params);
		SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
		List<Map<String,Object>> ordersOpLog = new ArrayList<>();
		if(ordersOPLogList!=null&&ordersOPLogList.size()>0){
			for (ShopOrdersOplog shopOrdersOplog : ordersOPLogList) {
				Map<String,Object> map = new HashMap<>();
				map.put("ordersNo", shopOrdersOplog.getOrdersNo());
				map.put("shopName", shopOrdersOplog.getShopName());
				if("1".equals(shopOrdersOplog.getShopInfoType()) && shopOrdersOplog.getShopInfoType()=="1"){
					map.put("shopInfoType","自营店铺");
				}else{
					map.put("shopInfoType","入驻店铺");
				}
				switch (shopOrdersOplog.getOrdersOperateState()){
					case 0:
						map.put("ordersOperateState","生成订单");
						break;
					case 1:
						map.put("ordersOperateState","未付款");
						break;
					case 3:
						map.put("ordersOperateState","正在配货");
						break;
					case 4:
						map.put("ordersOperateState","已发货");
						break;
					case 5:
						map.put("ordersOperateState","待评价");
						break;
					case 6:
						map.put("ordersOperateState","已取消");
						break;
					case 7:
						map.put("ordersOperateState","异常订单");
						break;
					case 9:
						map.put("ordersOperateState","已评价");
						break;
				}
				map.put("operatorName", shopOrdersOplog.getOperatorName());
				map.put("operatorTime", shopOrdersOplog.getOperatorTime());
				ordersOpLog.add(map);
			}
		}
		String fileName ="订单日志";
		String columnNames[] = {"订单号","店铺名称","店铺类型","订单操作","操作人","操作时间"};
		String keys[] = {"ordersNo","shopName","shopInfoType","ordersOperateState","operatorName","operatorTime"};
		try {
			ExportExcel.download(fileName,ordersOpLog,columnNames,keys,request,response);
		} catch (IOException e) {
			logger.error("导出Excel失败！");
		}

	}

}
